/**
 *@Description
 *@author your name
 *@createDate 2011-7-8 上午06:32:59
 *@version V1.0
 */
package com.welldone.service.order;

import java.util.List;
import java.util.Map;
import java.util.UUID;

import com.welldone.service.DBService;
import com.welldone.util.CommonUtil;

public class OrderManager {

	/**
	 * 添加预定
	 *@author your name
	 *@createDate 2011-7-8
	 *@param map
	 *@return
	 */
	public boolean addOrder(Map map){
		boolean flag = false;
		String sql = "insert into t_order(orderId,orderDate,orderPerson,orderPhone,orderMemo,orderStartTime,orderStatus) values(?,sysdate,?,?,?,?,'0')";
		String orderId = UUID.randomUUID().toString();
		Object[] args = {orderId,map.get("orderPerson"),map.get("orderPhone"),map.get("orderMemo"),map.get("orderStartTime")};
		int result = DBService.getJdbcTemplate().update(sql, args);
		if(result !=-1){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 修改预定
	 *@author your name
	 *@createDate 2011-7-8
	 *@param map
	 *@return
	 */
	public boolean updateOrder(Map map){
		boolean flag = false;
		String sql = "update t_order set orderPerson=?,orderPhone=?,orderMemo=?,orderStartTime=? where orderId = ?";
		Object[] args = {map.get("orderPerson"),map.get("orderPhone"),map.get("orderMemo"),map.get("orderStartTime"),map.get("orderId")};
		int result = DBService.getJdbcTemplate().update(sql, args);
		if(result !=-1){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 删除预定,或者更新完成预定
	 *@author your name
	 *@createDate 2011-7-8
	 *@param map
	 *@return
	 */
	public boolean deleteOrder(Map map){
		boolean flag = false;
		String sql = "update t_order set orderStatus=? where orderId = ?";
		Object[] args = {map.get("orderStatus"),map.get("orderId")};
		int result = DBService.getJdbcTemplate().update(sql, args);
		if(result !=-1){
			flag = true;
		}
		return flag;
	}
	
	/**
	 * 根据条件查询预定信息
	 *@author your name
	 *@createDate 2011-7-8
	 *@param queryMap
	 *@return
	 */
	public List getOrderByCondition(Map queryMap){
		StringBuffer where  = new StringBuffer();
		if(null!=queryMap){
			if(CommonUtil.equalsNotNull(queryMap.get("startTime"))){
				where.append(" and orderDate>="+queryMap.get("startTime"));
			}
			
			if(CommonUtil.equalsNotNull(queryMap.get("endTime"))){
				where.append(" and orderDate<="+queryMap.get("endTime"));
			}
			
			if(CommonUtil.equalsNotNull(queryMap.get("orderStartTime"))){
				where.append(" and orderStartTime>="+queryMap.get("orderStartTime"));
			}
			
			if(CommonUtil.equalsNotNull(queryMap.get("orderEndTime"))){
				where.append(" and orderStartTime<="+queryMap.get("orderEndTime"));
			}
			
			if(CommonUtil.equalsNotNull(queryMap.get("orderStatus"))){
				where.append(" and orderStatus ="+queryMap.get("orderStatus"));
			}
		}
		String sql = "select * from t_order where 1=1 "+where.toString();
		return DBService.getJdbcTemplate().queryForList(sql);
	}
	
	
}
